<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 
      计算属性: 会根据一个或者多个现有的响应式数据，来计算或者过滤得到一个新的结果
     -->
    <div id="app">
      <h2>计算属性</h2>
      <p>count: {{ count }}</p>
      <p>doubleCount: {{ doubleCount }}</p>
      <button @click="count++">btn</button>

      <hr />

      <ul>
        <li v-for="item in oddList" :key="item">{{ item }}</li>
      </ul>

      <hr />

      <p>str: {{ reverseStr }}</p>
      <p>str: {{ reverseStr }}</p>
      <p>str: {{ reverseStr }}</p>
      <p>str: {{ reverseStr }}</p>
      <p>str: {{ reverseStr }}</p>

      <!-- <button @click="reverseStr = 'ok'">btn</button> -->

      <hr />

      <input type="text" v-model="firstname" /> +
      <input type="text" v-model="lastname" /> = {{ fullName }}

      <hr />

      <ul>
        <li v-for="(item, index) in arr" :key="index">
          {{item.product}}的库存还剩<input type="number" v-model="item.count" />
        </li>
      </ul>
      <p>总库存还剩{{total}}</p>
    </div>

    <script src="./vue.js"></script>
    <script>
      Vue.createApp({
        data() {
          return {
            count: 10,
            list: [1, 2, 3, 4, 5, 6, 7, 8, 9],
            str: "hello vue!",
            firstname: "zhang",
            lastname: "san",
            arr: [
              {
                product: "🍎",
                count: 6,
              },
              {
                product: "🍌",
                count: 10,
              },
              {
                product: "🍉",
                count: 12,
              },
            ],
          };
        },
        computed: {
          doubleCount() {
            return this.count * 2;
          },
          oddList() {
            return this.list.filter((item) => item % 2);
          },
          reverseStr() {
            console.log(1111);
            return this.str.split("").reverse().join("");
          },
          fullName() {
            return this.firstname + this.lastname + this.doubleCount;
          },
          total() {
            return this.arr.reduce((sum, item) => {
              return (sum += item.count);
            }, 0);
          },
        },

        // methods: {
        //   reverseStr() {
        //     console.log(1111);
        //     return this.str.split("").reverse().join("");
        //   },
        // },
      }).mount("#app");
    </script>
  </body>
</html>
